----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 55313 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 1024
SPELL.m_sName = "Blade Toss"
SPELL.m_sToolTip = [[You throw your blade
					at the target, bleeding 
					them for 5 seconds and 
					then pulling them towards
					you.]]
SPELL.m_sIcon = "devin/blackbetty/spells/warrior/bladetoss.png"
SPELL.m_iCoolDown = 10
SPELL.m_iManaCost = 0
SPELL.m_bRequiresTarget = true
SPELL.m_iSpellType = SPELLTYPE_BRAWLER
SPELL.m_bRecastWhileCasting = false

---------------------------------------------------------------------------------------

local range = 100
local weapondamage = 20

function SPELL:Cast( pl, target )
	if(((target:GetPos()) - pl:GetPos()):Length() <= self.m_iRange)then
		
		if SERVER then
			if(!pl.Shield)then
				HandleCast(pl, target, 21312, true)
			else
				
			end
		end
		
		net.Start("IncurGCD")
		net.WriteFloat(5)
		net.Send(pl)
		pl:EmitSound("devin/blackbetty/weapons/slam/swing_"..math.random(1,3)..".wav", 100, 100+math.random(-10,10))
		pl:DoAnimationEvent(ACT_HL2MP_GESTURE_RANGE_ATTACK_MELEE)
		local myhitdelay = ((pl:GetPos() - target:GetPos()):Length()/1000)
		local shotProjectile = LaunchProjectile(pl, target, 0, "pummel_hit", "", "whirlwind", "", 60, 500, "models/devin/rpgdm_weapons/basic/bm_sword1.mdl")
		shotProjectile.IsSword = true
		print(shotProjectile:GetClass())
		local CurRopeSlack = 64
		--Create chain!
		local rope = ents.Create( "keyframe_rope" )
			rope:SetPos( pl:GetPos() )
			rope:SetKeyValue( "Width", 2)
			rope:SetKeyValue( "Slack", CurRopeSlack)
			rope:SetKeyValue( "Collide", 1)
			rope:SetKeyValue( "RopeMaterial", "cable/pvkchains3" )
			rope:SetEntity( "StartEntity", pl)
			rope:SetKeyValue( "StartOffset", tostring((pl:GetAngles():Forward()*16)+Vector(0, 0, 48)))
			rope:SetKeyValue( "StartBone", pl:LookupBone("ValveBiped.Bip01_R_Hand") )
			rope:SetEntity( "EndEntity", target)
			rope:SetKeyValue( "EndOffset", tostring(Vector(0, 0, 0)) )
		rope:Spawn()
        rope:Activate()
		rope:SetEntity( "EndEntity", shotProjectile)
        shotProjectile:DeleteOnRemove( rope )
		shotProjectile:SetAngles(pl:GetAngles()+Angle(180, 90, 180))
		--
		timer.Simple(myhitdelay, function()
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			shotProjectile.Travelling = false
			shotProjectile.IsSword = true
			shotProjectile:SetMoveType(MOVETYPE_NONE)
			shotProjectile:SetParent(target)
			shotProjectile:SetAngles((pl:GetPos()+Vector(0, 0, 32) - target:GetPos()):Angle() + Angle(0, 0, 0))
			target:EmitSound("devin/blackbetty/weapons/heroic_strike/hit_"..math.random(1,3)..".wav", 160, 100)
			target:EmitSound("devin/blackbetty/weapons/RendTarget.wav", 160, 100)
			ParticleEffect( "blood_erupt", target:GetPos() + Vector(0, 0, 16) + (target:GetAngles():Forward()*7), Angle(0,0,0), target )
			target.Speed = true	
				target.SpeedMod = -1
				target.SpeedModDuration = 2
				target.SpeedType = "Charge"
			target:SetVelocity(target:GetVelocity()*-1)
			rope:SetKeyValue( "Slack", 64)
			AddEffect(target, 4, "BladeThrowDoT", {pl, DMG_SLASH}, 10, 3, 1)
			ParticleEffect( "player_blood", target:GetPos() + Vector(0, 0, 64) + (target:GetAngles():Forward()*24), Angle(0,0,0), target )
			ParticleEffectAttach( "blood_erupt_constant", PATTACH_POINT_FOLLOW, target, target:LookupAttachment("eyes"))
			timer.Simple(2.5, function()
				pl:EmitSound("devin/blackbetty/weapons/GougeTarget.wav", 100, 100)
				pl:EmitSound("devin/blackbetty/weapons/Disarm.wav", 100, 100)
				target:StopParticles()
				timer.Simple(0.1, function()
					rope:SetKeyValue( "Slack", -128)
					local dmginfo = DamageInfo()
						dmginfo:SetDamage( 20 ) --50 damage
						dmginfo:SetDamageType( DMG_GENERIC ) --Bullet damage
						dmginfo:SetAttacker( pl ) --First player found gets credit
					target:TakeDamageInfo( dmginfo ) 
				end)
				local subtractedvectors = (pl:GetPos()+(Vector(0, 0, 72)) - target:GetPos())
				local subtractedvectorsnorm = (pl:GetPos()+(Vector(0, 0, 72)) - target:GetPos())
				subtractedvectorsnorm:Normalize()
				target:SetVelocity(subtractedvectorsnorm*(4*subtractedvectors:Length()+128))
				timer.Simple(0.2, function()
					target:SetVelocity(target:GetVelocity()*-1)
					shotProjectile:Remove()
				end)
			end)
		end)
	end
end




